//
//  TeamDetailViewController.swift
//  FootballDataRecord
//
//  Created by Xx Jiang on 2020/1/7.
//  Copyright © 2020 Xx Jiang. All rights reserved.
//

import UIKit

class TeamDetailViewController: UIViewController,UITableViewDelegate,UITableViewDataSource {
    @IBOutlet weak var teamNameLabel: UILabel!
    @IBOutlet weak var tableview: UITableView!
    var teamType:AddGameDetailViewController.TeamType!
    var teamModel:TeamModel?
    var matchModel:MatchModel?
    var playerList:[PlayerModel]?
    override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(forName: NSNotification.Name(rawValue: "changeTeam`sPlayerData"), object: nil, queue: .main) { (notification) in
            //在这里访问数据库查询数据刷新列表
            self.teamType = (notification.userInfo!["team"] as! AddGameDetailViewController.TeamType)
//            self.teamNameLabel.text = (notification.userInfo!["teamName"] as! String)
            self.matchModel = (notification.userInfo!["matchModel"] as! MatchModel)
            self.reloadData()
        }
        setUI()
    }
    
    
    func reloadData(){
        if matchModel != nil && (!DYStringIsEmpty(value: matchModel?.matchTeamA as AnyObject?)) && (!DYStringIsEmpty(value: matchModel?.matchTeamB as AnyObject?)) {
//            let teamId = self.teamType!.rawValue == 1 ?
            teamModel = TeamModel.getTeamModelWithTeamID(teamId: ((self.teamType!.rawValue == 1) ? (matchModel?.matchTeamB):(matchModel?.matchTeamA)) ?? "")
            if teamModel != nil {
                self.teamNameLabel.text = teamModel?.teamName
                playerList = TeamModel.getTeamPlayerListWihtTeamID(teamID: teamModel!.teamID!)
                tableview.reloadData()
            }
       
    }
        
}
    
  
    func setUI()  {
        self.tableview.delegate = self
        self.tableview.dataSource = self
        self.tableview.register(UINib.init(nibName: "PlayerDataTableViewCell", bundle: nil), forCellReuseIdentifier: "PlayerDataTableViewCell")
    }
    override func viewWillAppear(_ animated: Bool) {
        
    }

    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if playerList != nil {
            return playerList!.count > 0 ? playerList!.count:20
        }
        return 20
    }
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "PlayerDataTableViewCell") as! PlayerDataTableViewCell
        cell.backgroundColor = indexPath.row % 2 == 0 ? #colorLiteral(red: 0.1607843137, green: 0.1568627451, blue: 0.1568627451, alpha: 1) : #colorLiteral(red: 0.1725490196, green: 0.1725490196, blue: 0.1725490196, alpha: 1)
        cell.selectionStyle = .none
        if playerList != nil && playerList!.count > 0 {
            cell.setCell(model: playerList![indexPath.row])
        }
        return cell
    }
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.00001
    }
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        return 30.0
    }
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        let view = Bundle.main.loadNibNamed("TeamInfoSectionHeaderView", owner: self, options: nil)?.last as! UIView
        return view
    }
    
    

}
